//
//  BannerDetailsViewController.m
//  ZSYH
//
//  Created by 主事丫环 on 2019/11/14.
//  Copyright © 2019 魏堰青. All rights reserved.
//

#import "BannerDetailsViewController.h"
#import <WebKit/WebKit.h>
#import <JavaScriptCore/JavaScriptCore.h>
#import "BBuyNowShopDetailVC.h"

@interface BannerDetailsViewController ()<WKNavigationDelegate,WKUIDelegate,UIWebViewDelegate>
@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) NSString *urlString;

@end

@implementation BannerDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.titleStr;
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    
    if (strIsEmpty(_urlString)) {
        NSString *urlStr = [NSString stringWithFormat:@"%@%@", bannerImage, self.bannerId];
        _urlString = [NSString stringWithFormat:@"%@", urlStr];
        NSLog(@"%@--------轮播图---------", _urlString);
    }
    
    self.webView = [[UIWebView alloc]initWithFrame:self.view.frame];
    self.webView.scalesPageToFit = YES;
    self.webView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.webView];
    
    NSURL *url = [NSURL URLWithString:self.urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
    
    self.webView.delegate = self;
    // Do any additional setup after loading the view.
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [MBProgressHUD hideHUDForView:self.view animated:YES];

    JSContext *content = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    NSLog(@"%@", content);
    
    //立即预订
    content[@"onbuy"] = ^() {
        NSLog(@"js调用oc---------begin--------跳转商品详情");
        dispatch_async(dispatch_get_main_queue(), ^{
            BBuyNowShopDetailVC *vc = [[BBuyNowShopDetailVC alloc] init];
            vc.shopID = self.partsId;
            [self.navigationController pushViewController:vc animated:YES];
        });
    };
}

- (UIWebView *)webView {
    if (!_webView) {
        self.webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
        self.webView.dataDetectorTypes = UIDataDetectorTypeAll;
    }
    return _webView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
